package com.zmn.plat.admin.controller.orderconfig.track.category;

import java.util.List;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategory;
import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategoryQuery;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description ：跟单分类
 * @since 2020/11/17 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/orderconfig/trackCategory/")
@Slf4j
public class OrderTrackCategoryListController extends OrderTrackCategoryBaseController {

    /**
     * 描述: 跟单分类列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author tanbiao
     * @since 2020/11/17
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/trackCategory/list.action")
    public ModelAndView list(@ModelAttribute OrderTrackCategoryQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/track/category/list");
        List<OrderTrackCategory> cancelCategories = orderTrackCategoryService.listPageByQuery(query);
        mav.addObject("items", cancelCategories);
        mav.addObject("vo", query);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param categId
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2020/11/17
     * @modifier
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/orderconfig/trackCategory/detail.action")
    public ModelAndView detail(@RequestParam(value = "categId", defaultValue = "0") Integer categId) {
        ModelAndView mav = new ModelAndView("orderconfig/track/category/detail");
        OrderTrackCategory categ = null;
        if (categId == null || categId == 0) {
            categ = new OrderTrackCategory();
        } else {
            categ = Optional.ofNullable(orderTrackCategoryService.findByKey(categId)).orElse(new OrderTrackCategory());
        }
        if (categ.getSort() == null) {
            Integer sort = orderTrackCategoryService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            categ.setSort(sort);
        }
        mav.addObject("categ", categ);
        return mav;
    }
}
